package com.lyy.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.lyy.entity.Chapter;
import com.lyy.Vo.ChapterVo;
import com.lyy.Vo.ChapterVoPage;
import com.lyy.entity.param.ChapterInsertParam;
import com.lyy.entity.param.ChapterUpdateParam;
import com.lyy.result.Result;
import com.lyy.service.ChapterService;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Data
@ResponseBody
@RequestMapping("/api/v1/chapter")
public class ChapterController {
    public ChapterService chapterService;
    @RequestMapping("/select-by-chapter-id/{chapter-id}")
    public Result selectByChapterId(@PathVariable("chapter-id") Integer chapterId){
        Chapter chapter=chapterService.selectByChapterId(chapterId);
        if (ObjectUtil.isEmpty(chapter)) {
            return Result.failure();
        }
        ChapterVo chapterVo=new ChapterVo();
        BeanUtil.copyProperties(chapter, chapterVo);
        return Result.success(chapterVo);
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody ChapterInsertParam chapterParam){
        Chapter chapter=BeanUtil.copyProperties(chapterParam,Chapter.class);
        int result =chapterService.insertByChapterId(chapter);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/update")
    public Result update(@RequestBody ChapterUpdateParam chapterUpdateParam){
        Chapter chapter=BeanUtil.copyProperties(chapterUpdateParam,Chapter.class);
        int result=chapterService.updateByChapterId(chapter);
        return result>0?Result.success():Result.failure();
    }
    @RequestMapping("/list")
    public Result list(){
        List<Chapter> chapters=chapterService.selectAllChapter();
        if (ObjectUtil.isEmpty(chapters)) {
            return Result.failure();
        }
        List<ChapterVo> chapterVos = BeanUtil.copyToList(chapters, ChapterVo.class);
        return Result.success(chapterVos);
    }
    @RequestMapping("/delete-by-chapter-id/{chapter-id}")
    public Result deleteByBannerId(@PathVariable("chapter-id") Integer chapterId) {
        int result = chapterService.deleteByChapterId(chapterId);
        return result > 0 ? Result.success() : Result.failure();
    }
    @RequestMapping("/delete-by-chapter-ids")
    public Result deleteByBannerIds(@RequestParam("banner-ids") Integer[] chapterIds) {
        int result = chapterService.deleteByChapterIds(List.of(chapterIds));
        return result > 0 ? Result.success() : Result.failure();
    }

    @RequestMapping("/page")
    public Result page(@RequestParam("page") Integer page,
                       @RequestParam("size") Integer size) {
        PageInfo<Chapter> result = chapterService.selectBypage(page, size);
        if (ObjectUtil.isEmpty(result)) {
            return Result.failure();
        }
       ChapterVoPage chapterVoPage =new ChapterVoPage();
       chapterVoPage.setPage(result.getPageNum());
       chapterVoPage.setSize(result.getPageSize());
       chapterVoPage.setTotal(result.getTotal());
       chapterVoPage.setPages(result.getPages());
       chapterVoPage.setChapterVos(BeanUtil.copyToList(result.getList(), ChapterVo.class));
        return Result.success(chapterVoPage);
    }


}
